package com.jean.business.user;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.Logger;

import com.jean.business.bean.ConfigInfo;
import com.jean.business.bean.UserInfo;
import com.jean.common.ConstantConfigs;
import com.jean.init.SystemConfig;
import com.jean.util.LoggerFactory;

public class UserSessionManage
{
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger();
    
    /**
     * <一句话功能简述>刷新session
     * <功能详细描述>
     * @param request 请求
     * @see [类、类#方法、类#成员]
     */
    public static void refreshSession(HttpServletRequest request)
    {
        if (request == null)
        {
            logger.warn("the HttpServletRequest is null.");
            return;
        }
        if (request.getSession() == null)
        {
            logger.warn("the HttpSession is null.");
            request.getSession(true);
            return;
        }
        // 遍历session内容，存入Map
        Map<String, Object> sessionMap = new HashMap<>();
        Enumeration<String> enums = request.getSession().getAttributeNames();
        while (enums.hasMoreElements())
        {
            String sessionName = (String)enums.nextElement();
            sessionMap.put(sessionName,
                request.getSession().getAttribute(sessionName));
        }
        request.getSession().invalidate();
        request.getSession(true);
        Iterator iterator = sessionMap.entrySet().iterator();
        while (iterator.hasNext())
        {
            Map.Entry entry = (Map.Entry)iterator.next();
            if("JSESSIONID".equals(entry.getKey()))
            {
                continue;
            }
            logger.info("session attrbutes: " + entry.getKey());
            request.getSession().setAttribute((String)entry.getKey(), entry.getValue());
        }
    }
    
    /**
     * <一句话功能简述>管理员是否在线
     * <功能详细描述>
     * @param request
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean isUserOnline(HttpServletRequest request)
    {
        if (request == null)
        {
            logger.warn("the HttpServletRequest is null.");
            return false;
        }
        if (request.getSession() == null)
        {
            logger.warn("the HttpSession is null.");
            return false;
        }
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute(ConstantConfigs.SESSION_USERINFO);
        if(userInfo == null)
        {
            logger.warn("the userInfo in session is null.");
            return false;
        }
        return true;
    }
    
    /**
     * <一句话功能简述>获取需要返回给前台页面的配置项
     * <功能详细描述>
     * @return 配置项
     * @see [类、类#方法、类#成员]
     */
    public static ConfigInfo[] getWebConfigs()
    {
        List<String> webConfigNames = new ArrayList<String>();
        webConfigNames.add(ConstantConfigs.REGEXP_EMAIL);
        webConfigNames.add(ConstantConfigs.REGEXP_PHONENUMBER);
        
        ConfigInfo[] configs = new ConfigInfo[webConfigNames.size()];
        for (int i = 0; i < configs.length; i++)
        {
            configs[i] = SystemConfig.getConfig(webConfigNames.get(i));
        }
        return configs;
    }
}
